# Clean up
rm(list = ls())
Sys.setenv(LANG = "en")

# Set working directory: please set your own
#setwd("~/Dropbox/sanctions_replication/")

# Load necessary packages
library(tidyverse)
library(lubridate)
library(readstata13)

# Import dataset
data <- read.dta13("data/survey_clean.dta")

# Make the plot
figureb10 <- data %>%
  select("intent_phis_vox_num", "intent_verb_vox_num", "intent_insult_vox_num", "no_intent_vox_num", 
         "intent_avoid_vox_num", "intent_deny_vox_num", "intent_gossip_vox_num",
         "exp_intent_phis_vox_num", "exp_intent_verb_vox_num", "exp_intent_insult_vox_num", "exp_no_intent_vox_num", 
         "exp_intent_avoid_vox_num", "exp_intent_deny_vox_num", "exp_intent_gossip_vox_num",
         "intent_phis_up_num","intent_verb_up_num", "intent_insult_up_num",  "no_intent_up_num",
         "intent_avoid_up_num", "intent_deny_up_num", "intent_gossip_up_num",
         "exp_intent_phis_up_num","exp_intent_verb_up_num", "exp_intent_insult_up_num",  "exp_no_intent_up_num",
         "exp_intent_avoid_up_num", "exp_intent_deny_up_num", "exp_intent_gossip_up_num",
         "intent_phis_psoe_num", "intent_verb_psoe_num", "intent_insult_psoe_num",  "no_intent_psoe_num",
         "intent_avoid_psoe_num", "intent_deny_psoe_num","intent_gossip_psoe_num",
         "exp_intent_phis_psoe_num", "exp_intent_verb_psoe_num", "exp_intent_insult_psoe_num",  "exp_no_intent_psoe_num",
         "exp_intent_avoid_psoe_num", "exp_intent_deny_psoe_num","exp_intent_gossip_psoe_num") %>%
  `colnames<-`(c("Vox_Physical", "Vox_Verbal", "Vox_Insult", "Vox_Nothing",
                 "Vox_Avoid", "Vox_Deny", "Vox_Gossip", 
                 "Vox_Physical_Others", "Vox_Verbal_Others", "Vox_Insult_Others", "Vox_Nothing_Others",
                 "Vox_Avoid_Others", "Vox_Deny_Others", "Vox_Gossip_Others", 
                 "PSOE_Physical", "PSOE_Verbal", "PSOE_Insult","PSOE_Nothing",
                 "PSOE_Avoid", "PSOE_Deny", "PSOE_Gossip", 
                 "PSOE_Physical_Others", "PSOE_Verbal_Others", "PSOE_Insult_Others", "PSOE_Nothing_Others",
                 "PSOE_Avoid_Others", "PSOE_Deny_Others", "PSOE_Gossip_Others", 
                 "UP_Physical", "UP_Verbal", "UP_Insult", "UP_Nothing",
                 "UP_Avoid", "UP_Deny", "UP_Gossip", 
                 "UP_Physical_Others", "UP_Verbal_Others", "UP_Insult_Others","UP_Nothing_Others",
                 "UP_Avoid_Others", "UP_Deny_Others", "UP_Gossip_Others" 
  )) %>%
  pivot_longer(cols = c(Vox_Physical:UP_Nothing_Others),
               names_to = "Group") %>%
  group_by(Group) %>%
  mutate(n = sum(!is.na(value))) %>%
  mutate(mean = mean(value, na.rm = T)) %>%
  mutate(sd = sd(value, na.rm = T)) %>%
  mutate(std_error = sd/sqrt(n)) %>%
  mutate(ci_upper = mean + 1.96 * std_error) %>%
  mutate(ci_lower = mean - 1.96 * std_error) %>%
  ungroup() %>%
  mutate(Outcome = ifelse(Group %in% c("Vox_Physical", "Vox_Verbal", "Vox_Insult",
                                       "Vox_Avoid", "Vox_Deny", "Vox_Gossip", "Vox_Nothing",
                                       "PSOE_Physical", "PSOE_Verbal", "PSOE_Insult",
                                       "PSOE_Avoid", "PSOE_Deny", "PSOE_Gossip", "PSOE_Nothing",
                                       "UP_Physical", "UP_Verbal", "UP_Insult",
                                       "UP_Avoid", "UP_Deny", "UP_Gossip", "UP_Nothing"), "Own willingness to sanction", "Perceptions of others' willingness to sanction")) %>%
  mutate(Nothing = ifelse(Group %in% c("Vox_Nothing", "Vox_Nothing_Others",
                                       "UP_Nothing", "UP_Nothing_Others",
                                       "PSOE_Nothing", "PSOE_Nothing_Others"), 1, 0)) %>%
  mutate(Party = ifelse(Group %in% c("Vox_Physical", "Vox_Verbal", "Vox_Insult",
                                     "Vox_Avoid", "Vox_Deny", "Vox_Gossip", "Vox_Nothing",
                                     "Vox_Physical_Others", "Vox_Verbal_Others", "Vox_Insult_Others",
                                     "Vox_Avoid_Others", "Vox_Deny_Others", "Vox_Gossip_Others", "Vox_Nothing_Others"), "Vox", 
                        ifelse(Group %in% c("UP_Physical", "UP_Verbal", "UP_Insult",
                                            "UP_Avoid", "UP_Deny", "UP_Gossip", "UP_Nothing",
                                            "UP_Physical_Others", "UP_Verbal_Others", "UP_Insult_Others",
                                            "UP_Avoid_Others", "UP_Deny_Others", "UP_Gossip_Others", "UP_Nothing_Others"), "Podemos",
                               ifelse(Group %in% c("PSOE_Physical", "PSOE_Verbal", "PSOE_Insult",
                                                   "PSOE_Avoid", "PSOE_Deny", "PSOE_Gossip", "PSOE_Nothing",
                                                   "PSOE_Physical_Others", "PSOE_Verbal_Others", "PSOE_Insult_Others",
                                                   "PSOE_Avoid_Others", "PSOE_Deny_Others", "PSOE_Gossip_Others", "PSOE_Nothing_Others"), "PSOE", NA)))) %>%
  mutate(Sanction = ifelse(Group %in% c("Vox_Physical", "UP_Physical", "PSOE_Physical", "Vox_Physical_Others", "UP_Physical_Others", "PSOE_Physical_Others"), "Physical",
                           ifelse(Group %in% c("Vox_Verbal", "UP_Verbal", "PSOE_Verbal", "Vox_Verbal_Others", "UP_Verbal_Others", "PSOE_Verbal_Others"), "Verbal",
                                  ifelse(Group %in% c("Vox_Insult", "UP_Insult", "PSOE_Insult", "Vox_Insult_Others", "UP_Insult_Others", "PSOE_Insult_Others"), "Insult",
                                         ifelse(Group %in% c("Vox_Avoid", "UP_Avoid", "PSOE_Avoid", "Vox_Avoid_Others", "UP_Avoid_Others", "PSOE_Avoid_Others"), "Avoid\ninteraction",
                                                ifelse(Group %in% c("Vox_Deny", "UP_Deny", "PSOE_Deny", "Vox_Deny_Others", "UP_Deny_Others", "PSOE_Deny_Others"), "Deny help",
                                                       ifelse(Group %in% c("Vox_Gossip", "UP_Gossip", "PSOE_Gossip", "Vox_Gossip_Others", "UP_Gossip_Others", "PSOE_Gossip_Others"), "Gossip",
                                                              ifelse(Group %in% c("Vox_Nothing", "UP_Nothing", "PSOE_Nothing", "Vox_Nothing_Others", "UP_Nothing_Others", "PSOE_Nothing_Others"), "Do nothing",
                                                                     NA)))))))) %>%
  mutate(Outcome = fct_reorder(Outcome, Nothing, .desc = TRUE)) %>%
  ggplot(aes(x = Sanction, y = value, color = Party)) +
  scale_color_manual(values = c("purple", "red", "green4")) +
  theme_bw() +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.1, position = position_dodge(width = 0.5)) + 
  geom_point(aes(y = mean), position = position_dodge(width = 0.5)) +
  # theme(legend.position = "none") +
  facet_wrap(Outcome ~ ., scales = "free_y") +  
  labs(x = "", y = "") 
figureb10

ggsave("figures/figureb10.png", plot = figureb10, width = 10, height = 15, units = "cm")